package sketch.specs;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.Comment;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SketchedMethodCreatorTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(SketchedMethodCreatorTest.class);
	}
	
	public void testConstructor() {
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		Map<Comment, SketchedBlock> sketchedBlocks = creator.getSketchedBlocks();
		System.out.println(sketchedBlocks.size());
		assertTrue(sketchedBlocks.size() == 10);
		
		int repeat=0, chooseone = 0, chooseall = 0;
		
		for(SketchedBlock block : sketchedBlocks.values()) {
			System.out.println("----------");
			System.out.println("start pos: " + block.getStartPosition() + ", end pos: " + block.getEndPosition());
			System.out.println(block.getClass());
			System.out.println(block.getRealBlock());
			System.out.println();
			if(block.getClass().equals(SketchedChooseAllBlock.class)) {
				chooseall++;
			}
			if(block.getClass().equals(SketchedChooseOneBlock.class)) {
				chooseone++;
			}
			if(block.getClass().equals(SketchedRepeatBlock.class)) {
				repeat++;
			}
		}
		
		assertTrue(chooseall == 2);
		assertTrue(chooseone == 5);
		assertTrue(repeat == 3);
	}
	
	public void testCreateSketchedMethods() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> sketchedMethods = creator.createSketchedMethodList();
		
		assertTrue(sketchedMethods.size() == 7);
		
		for(SketchedMethodDeclaration sketchedMethod : sketchedMethods) {
			System.out.println(sketchedMethod.method_declaration.getName().getIdentifier());
			System.out.println("  " + sketchedMethod.method_declaration.getStartPosition() + ",  "
					+ (sketchedMethod.method_declaration.getStartPosition() + sketchedMethod.method_declaration.getLength()));
			System.out.println("    size of sub-blocks: " + sketchedMethod.numOfSubBlocks());
		}
	}
	
	public void testFindingFirstLevelBlocks() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> sketchedMethods = creator.createSketchedMethodList();
		
		assertTrue(sketchedMethods.size() == 7);
		
		List<SketchedBlock> all = new LinkedList<SketchedBlock>();
		for(SketchedMethodDeclaration sketchedMethod : sketchedMethods) {
			//System.out.println("    size of sub-blocks: " + sketchedMethod.numOfSubBlocks());
			all.addAll(sketchedMethod.getAllSubBlocks());
		}
		System.out.println("All blocks: " + all.size());
		for(SketchedBlock b : all) {
			System.out.println("  " + b.getStartPosition() + ",  " + b.getEndPosition());
		}
		List<SketchedBlock> firstLevel = SketchedBlock.findFirstLevelBlocks(all);
		System.out.println("First level blocks: " + firstLevel.size());
		
		assertEquals(8, firstLevel.size());
	}
	
	public void testBuildHierarchy() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> sketchedMethods = creator.createSketchedMethodList();
		
		assertTrue(sketchedMethods.size() == 7);
		
		for(SketchedMethodDeclaration sketchedMethod : sketchedMethods) {
			System.out.println(sketchedMethod.method_declaration.getName());
			System.out.println("  first level: " + sketchedMethod.numOfChildren());
			System.out.println("  total sub: " + sketchedMethod.numOfSubBlocks());
		}
	}
	
	public void testShowHierarchy() {
//		String filePath = "./tests/sketch/specs/samplecode.txt";;
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> sketchedMethods = creator.createSketchedMethodList();
		
		assertTrue(sketchedMethods.size() == 7);
		
		for(SketchedMethodDeclaration sketchedMethod : sketchedMethods) {
			sketchedMethod.printHierarchy();
		}
	}
	
	public void testShowHierarchy2() {
		System.out.println("executing test hierarchical");
		String filePath = "./testfiles/sketch/specs/Hierachical.java";;
		SketchedMethodsCreator creator = new SketchedMethodsCreator(filePath);
		List<SketchedMethodDeclaration> sketchedMethods = creator.createSketchedMethodList();
		
		assertTrue(sketchedMethods.size() == 1);
		
		for(SketchedMethodDeclaration sketchedMethod : sketchedMethods) {
			sketchedMethod.printHierarchy();
		}
	}
	
}
